﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Security.Cryptography;
using Sector7.Framework.Core;
using Sector7.Framework.Enums;

namespace Sector7.Framework.Utility
{
    /// <summary>
    /// 加解密类
    /// </summary>
    public class EncryptionUtil
    {
        /// <summary>
        /// MD5 加密
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="strEncrKey"></param>
        /// <returns></returns>
        public static string MD5Encrypt(string strText, string strEncrKey)
        {
            MemoryStream ms = null;
            CryptoStream cs = null;
            Byte[] byKey = { };
            Byte[] IV = { 0x01, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(strEncrKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                Byte[] inputByteArray = Encoding.UTF8.GetBytes(strText);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, des.CreateEncryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                return Convert.ToBase64String(ms.ToArray());
            }
            catch (Exception ex)
            {
                Log.WriteLine(LogSeverityType.ERR, "EmployeeSummaryControl", ":MD5Encrypt {0}", ex);
            }
            finally
            {
                GCUtil.CloseStream(ms);
                GCUtil.CloseStream(cs);
            }
            return strText;
        }

        /// <summary>
        /// MD5 解密
        /// </summary>
        /// <param name="strText"></param>
        /// <param name="sDecrKey"></param>
        /// <returns></returns>
        public static string MD5Decrypt(string strText, string sDecrKey)
        {
            char[] stBase = strText.ToCharArray();
            for (int i = 0; i < stBase.Length; i++)
            {
                if (stBase[i] == ' ')
                {
                    stBase[i] = '+';
                }
            }
            strText = "";
            for (int i = 0; i < stBase.Length; i++)
            {
                strText += stBase[i];
            }
            Byte[] byKey = { };
            Byte[] IV = { 0x01, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF };
            Byte[] inputByteArray = new byte[strText.Length];
            MemoryStream ms = null;
            CryptoStream cs = null;
            try
            {
                byKey = System.Text.Encoding.UTF8.GetBytes(sDecrKey.Substring(0, 8));
                DESCryptoServiceProvider des = new DESCryptoServiceProvider();
                inputByteArray = Convert.FromBase64String(strText);
                ms = new MemoryStream();
                cs = new CryptoStream(ms, des.CreateDecryptor(byKey, IV), CryptoStreamMode.Write);
                cs.Write(inputByteArray, 0, inputByteArray.Length);
                cs.FlushFinalBlock();
                System.Text.Encoding encoding = System.Text.Encoding.UTF8;
                return encoding.GetString(ms.ToArray());
            }
            catch (Exception ex)
            {
                Log.WriteLine(LogSeverityType.ERR, "EmployeeSummaryControl", ":MD5Encrypt {0}", ex);
            }
            finally
            {
                GCUtil.CloseStream(ms);
                GCUtil.CloseStream(cs);
            }
            return strText;
        }
    }
}
